﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using Blade.Metadata;
using System.Reflection;

namespace Blade.AotCompiler {
	public sealed class AotMetadataLocator : IMetadataLocator {
		public AotMetadataLocator() {
			m_SearchPaths = new Collection<string> {
				"./{0}.dll",
				"./{0}.exe",
			};
		}

		public Collection<string> SearchPaths { get { return m_SearchPaths; } }
		private Collection<string> m_SearchPaths;

		#region IMetadataLocator Members

		public Stream OpenAssembly(AssemblyName name) {
			if (File.Exists(name.CodeBase))
				return File.OpenRead(name.CodeBase);
			foreach (var item in SearchPaths) {
				var path = string.Format(
					item,
					name.Name,
					name.Version
				);
				if (File.Exists(path)) {
					return File.OpenRead(path);
				}
			}
			throw new FileNotFoundException();
		}

		#endregion
	}
}
